<template>
    <MainContent title="营销流程">
      <template #tab-bar>
        <el-radio-group v-model="tabPosition">
          <el-radio-button  plain label="list">按列表</el-radio-button>
          <el-radio-button  plain label="calendar">按日历</el-radio-button>
        </el-radio-group>
      </template>

      <process-list v-if="tabPosition == 'list'"></process-list>
      <process-calendar v-if="tabPosition == 'calendar'"></process-calendar>
    </MainContent>
</template>
<script>
import CustomerApi from "@/api/customerApi.js";
import processList from "./component/list.vue";
import processCalendar from "./component/calendar.vue";
import store from "@/store";
export default {
  name: "MatketingProcess",
  components: {
    processList,
    processCalendar,
  },
  filters: {
    fetchTypeFilters(val) {
      return {
        0: "本地拉取",
        1: "远程拉取",
      }[val];
    },
    startTypeFilters(val) {
      return {
        0: "单次执行",
        1: "直接执行",
        2: "延迟执行",
        3: "循环执行",
      }[val];
    },
    statusFilters(val) {
      return {
        0: "草稿", // 详情 复制 编辑 发布 删除
        1: "发布中", // 详情 复制 取消发布
        2: "运行中", // 详情 复制 暂停 强制结束
        "-1": "暂停中", // 详情 复制 开启 强制结束
        "-2": "已结束", // 详情 复制 删除
      }[val];
    },
  },
  // created(){

  // },
  data() {
    return {
      addGroup: false,
      tabPosition: "list",
      groupData: { id: null },
      formData: {},
      activityDetail: {},
      // buttonList: {
      //   draft: [], // 详情 复制 编辑 发布 删除
      //   pushing: "发布中", // 详情 复制 取消发布
      //   pushed: "运行中", // 详情 复制 暂停 强制结束
      //   pause: "暂停中", // 详情 复制 开启 强制结束
      //   end: "已结束", // 详情 复制 删除
      // },
      buttons: new Map(),
      overviewList: [
        {
          name: "全部",
          key: "total",
        },
        {
          name: "运行中",
          key: "processing",
        },
        {
          name: "暂停中",
          key: "pause",
        },
        {
          name: "已结束",
          key: "stop",
        },
        {
          name: "草稿箱",
          key: "wait",
        },
      ],
      tableForm: {
        name: "",
      },
    };
  },
  watch: {
    insId: {
      handler(val) {
        this.canvasCurrent();
        // buttonList: {
        //   draft: [], // 详情 复制 编辑 发布 删除
        //   pushing: "发布中", // 详情 复制 取消发布
        //   pushed: "运行中", // 详情 复制 暂停 强制结束
        //   pause: "暂停中", // 详情 复制 开启 强制结束
        //   end: "已结束", // 详情 复制 删除
        this.buttons.set(0, ["info", "copy", "edit", "publish", "delete"]);
        this.buttons.set(1, ["info", "copy", "cancel-publish"]);
        this.buttons.set(2, ["info", "copy", "pushed", "stop"]);
        this.buttons.set(-1, ["info", "copy", "enable", "stop"]);
        this.buttons.set(-2, ["info", "copy", "delete"]);
        this.$nextTick(() => {
          this.onSearch();
        })
      },
      deep: true
    },
  },
  created() {
    this.canvasCurrent();
    // buttonList: {
    //   draft: [], // 详情 复制 编辑 发布 删除
    //   pushing: "发布中", // 详情 复制 取消发布
    //   pushed: "运行中", // 详情 复制 暂停 强制结束
    //   pause: "暂停中", // 详情 复制 开启 强制结束
    //   end: "已结束", // 详情 复制 删除
    this.buttons.set(0, ["info", "copy", "edit", "publish", "delete"]);
    this.buttons.set(1, ["info", "copy", "cancel-publish"]);
    this.buttons.set(2, ["info", "copy", "pushed", "stop"]);
    this.buttons.set(-1, ["info", "copy", "enable", "stop"]);
    this.buttons.set(-2, ["info", "copy", "delete"]);
  },

  methods: {
    showButton(status, type) {
      let btns = this.buttons.get(status);
      return btns.indexOf(type) !== -1;
    },
    // 暂停
    async pauseCanvas(row) {
      let rs = await CustomerApi.pauseCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已暂停");
        this.onSearch();
      }
    },

    pause(row) {
      this.$confirm("此操作将暂停该流程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.pauseCanvas(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    // 复制
    async copyCanvas(row) {
      let rs = await CustomerApi.copyCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已复制");
        this.onSearch();
      }
    },

    // 强制结束
    async stopCanvas(row) {
      let rs = await CustomerApi.stopCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已停止");
        this.onSearch();
      }
    },
    stop(row) {
      this.$confirm("此操作将强制结束该流程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.stopCanvas(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 发布
    async publishCanvas(row) {
      let rs = await CustomerApi.publishCanvas({ id: row.id });
      if (rs.code == 200) {
        this.$message.success("已发布");
        this.onSearch();
      }
    },
    // 取消发布
    async cancelPublishCanvas(row) {
      let rs = await CustomerApi.cancelPublishCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已发布");
        this.onSearch();
      }
    },
    cancelPublish(row) {
      this.$confirm("是否取消发布?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.cancelPublishCanvas(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    // 开启
    async recoveryCanvas(row) {
      let rs = await CustomerApi.recoveryCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已恢复开启");
        this.onSearch();
      }
    },

    // 删除
    async delCanvas(row) {
      console.log("[ row ] >", row);
      let rs = await CustomerApi.delCanvas({ id: row.id, name: row.name });
      if (rs.code == 200) {
        this.$message.success("已经删除");
        this.onSearch();
      }
    },

    del(row) {
      this.$confirm("此操作将删除该流程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delCanvas(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    async canvasCurrent() {
      let rs = await CustomerApi.canvasCurrent();
      if (rs.code == 200) {
        this.activityDetail = rs.payload;
      }
    },

    toEditor(row) {
      this.addGroup = true;
      this.groupData = JSON.parse(JSON.stringify(row));
    },
    resetForm() {
      this.tableForm = {
        name: "",
      };
      this.onSearch();
    },
    closeGroup(val, close) {
      this.addGroup = val;
      this.groupData = { id: null };
      if (!close) {
        this.onSearch();
      }
    },
    onSearch() {
      this.$nextTick(() => {
        this.$refs.customerGroupRef.search();
        this.canvasCurrent();
      });
    },
  },
  computed: {
    insId() {
      return store.state.insId.insId;
    },
  }
};
</script>

<style lang="scss" scoped>
.detail {
  &-title {
    height: 56px;
    line-height: 56px;
    padding: 0 22px;
    font-size: 16px;
    font-weight: 700;
    color: rgba(0, 0, 0, 0.65);
    border-bottom: 1px solid rgba(0, 0, 0, 0.09);
    span {
      font-weight: 400;
      color: rgba(0, 0, 0, 0.65);
    }
  }

  &-data {
    display: flex;
    align-items: center;
    padding: 24px;
    border-top: 1px solid rgba(0, 0, 0, 0.09);
    &-item {
      // flex: 1;
      width: 278px;
      text-align: center;
      font-size: 30px;
      font-weight: bold;
      line-height: 38px;
      p {
        font-size: 14px;
        font-weight: normal;
        line-height: 22px;
        margin-bottom: 5px;
      }
    }
  }
}
.data-content {
  width: 100%;
  height: 100px;
  display: flex;
  align-items: center;
  flex: 1;
  background: #f9faff;
}
.data-content:nth-child(1) {
  margin-right: 35px;
}
:deep(.el-radio-button .is-active) {
  background: #fff !important;
}
</style>

